// components/order-item/index.js
import {OrderModel} from '../../model/order'
var order = new OrderModel()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    model:Object
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    
    payAction:function(e){
      var data = e.currentTarget.dataset
      switch (data.item) {
        case "取消订单":
          this.cancel(data.id)
          break;
        case "立即付款":
          this.pay(data.id)
          break;
      
        default:
          break;
      }
      console.log(data)
    },

    pay(orderId){
      order.pay(orderId).then(
        res=>{
          if (res.code===0) {
            console.log(res.data)
            var data = JSON.parse(res.data);
            wx.requestPayment({
              timeStamp: data.timeStamp,
              nonceStr: data.nonceStr,
              package: data.package,
              signType: 'MD5',
              paySign: data.paySign,
              success (resp) {
                wx.showToast({
                  title: '支付成功',
                  icon: 'none'
                })
                this.triggerEvent("upData")
               },
            })
          }
        }
      )
    },
    
    cancel(orderId){
      wx.showModal({
        content: '是否取消订单',
        confirmText: '确认',
        confirmColor: '#F7859C',
        success: res=>{
          if (res.confirm) {
            order.cancel(orderId).then(
              res=>{
                if(res.code===0){
                  wx.showToast({
                    title: '订单已取消',
                    icon: 'none'
                  })
                  this.triggerEvent("upData")
                }
              }
            )
          }
        }
      })
    },
  },

  
})
